package org.itzheng.yuezhi.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Title:Okhttp的简单封装<br>
 * Description: <br>
 * Company: <br>
 *
 * @author ZhengYongdong
 * @email ItZheng@ZoHo.com
 * @date 2016/6/24 0024
 */
public class MyOkHttp {
    OkHttpClient client = new OkHttpClient();

    public static MyOkHttp getInstance() {
        return new MyOkHttp();
    }

    /**
     * get请求,没有请求头部
     *
     * @param url
     * @param argMap          请求参数
     * @param requestCallBack
     */
    public void getUrl(String url, Map<String, Object> argMap, RequestCallBack requestCallBack) {
        getUrl(url, new Header(null, null), argMap, requestCallBack);
    }

    /**
     * get请求,只有1个请求头
     *
     * @param url
     * @param header          请求头
     * @param argMap          请求参数
     * @param requestCallBack
     */
    public void getUrl(String url, Header header, Map<String, Object> argMap, RequestCallBack requestCallBack) {
        if (argMap == null || argMap.isEmpty()) {

        } else {
            int tempsize = url.length();
            for (Map.Entry<String, Object> entry : argMap.entrySet()) {
                if (tempsize == url.length()) {//如果相等,说明是第一个参数
                    url = url + "?" + entry.getKey() + "=" + entry.getValue().toString();
                } else {//不是第一个参数
                    url = url + "&" + entry.getKey() + "=" + entry.getValue().toString();
                }
            }
        }
        if (requestCallBack != null) {
            getAsync(url, header, null, requestCallBack);
        }
    }

    /**
     * get请求,一个或多个请求头
     *
     * @param url
     * @param headers         请求头集合
     * @param argMap          请求参数
     * @param requestCallBack
     */
    public void getUrl(String url, Map<String, Object> headers, Map<String, Object> argMap, RequestCallBack requestCallBack) {
        if (argMap == null || argMap.isEmpty()) {

        } else {
            int tempsize = url.length();
            for (Map.Entry<String, Object> entry : argMap.entrySet()) {
                if (tempsize == url.length()) {//如果相等,说明是第一个参数
                    url = url + "?" + entry.getKey() + "=" + entry.getValue().toString();
                } else {//不是第一个参数
                    url = url + "&" + entry.getKey() + "=" + entry.getValue().toString();
                }
            }
        }
        if (requestCallBack != null) {
            getAsync(url, null, headers, requestCallBack);
        }
    }

    /**
     * 发送get请求
     *
     * @param url
     * @param header          单个请求头
     * @param headers         多个请求头
     * @param requestCallBack
     */
    private void getAsync(String url, Header header, Map<String, Object> headers, final RequestCallBack requestCallBack) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers == null || headers.isEmpty()) {
            //参数为null
        } else {
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue().toString());
            }
        }
        if (header == null || TextUtils.isEmpty(header.getName()) || TextUtils.isEmpty(header.getValue())) {
            //请求头部不能为空

        } else {
            builder.addHeader(header.getName(), header.getValue());
        }
        Log.i("okhttp", "url=" + url);
        client.newCall(builder.build()).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                requestCallBack.onFailure(null, call.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                requestCallBack.onSuccess(result);
            }
        });

    }

    /**
     * 取消全部请求
     */
    public void cancelAll() {
        client.dispatcher().cancelAll();
    }

    /**
     * 请求回调接口
     */
    public interface RequestCallBack {
        /**
         * 请求失败返回的信息
         *
         * @param httpException 异常信息
         */
        void onFailure(Exception httpException, String describe);

        /**
         * 请求成功返回的信息
         *
         * @param result 请求到的字符串
         */
        void onSuccess(String result);
    }

    /**
     * 请求参数 header,当只有一个请求参数时,方便填写
     */
    public static class Header {
        private String name;
        private String value;

        public Header(java.lang.String name, java.lang.String value) {
            setName(name);
            setValue(value);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
